<?php
namespace app\admin\controller;
use x\Xbase;
class Base extends Xbase{
    public $userinfo = [];
    public $sys = [];
    public function __construct(){
        parent::__construct();
        clear_cookie_arr('js');
        $this->_sys();
        $this->_user();
        $this->_role();
    }

    //验证登陆
    public function _user(){
        $adminUser = m('AdminUser','xcom');
        $this->userinfo = $adminUser->isLogin();
        $this->assign('userinfo',$this->userinfo);
        if($this->c=='publics')
            return true;

        if(!$this->userinfo)
            r(url('publics/login'));
    }

    //通用
    public function _sys(){
        $this->sys = m('Deploy','xcom')->lists();
        $this->assign('sys',$this->sys);
    }

    //用户权限,菜单. 写入session: arr_id  菜单加上id的数组
    public function _role(){
        //检查权限
        if(!in_array($this->c, ['publics','index','api','com'])){

            if($this->userinfo&&$this->userinfo['type']==1) return true;

            $url = "/".$this->c."/".$this->a;
            if(session("?right") && !in_array($url, session("right"))){
                if(is_ajax()){
                    err('没有权限');
                }else{
                    err('没有权限');
                    //r(url("publics/logout"));
                }
            }
        }
        //t(session("?arr_id"),1);
        //菜单session
        //if(session("?arr_id")) return true;
        $menu = c('adminmenu');
        $arr_id = admin_menu_arr($menu,0);
        if($this->userinfo){
            if($this->userinfo['type']!=1 && $this->userinfo['admin_group_ids']){#非超级管理员
                $group_ids = array_filter(explode(',',$this->userinfo['admin_group_ids']));
                $rs = $this->db->query("select content from admin_group where id in (".implode(',',$group_ids).")");
                $temp = []; #权限
                foreach($rs as $v){
                    $temp = array_merge($temp,explode(",",$v['content']));
                }
                $temp = array_unique(array_filter($temp));
                $temp[] = "/index/index";
                $temp[] = "/adminuser/index";
                session("right", $temp);
                #去掉没有的权限
                foreach($arr_id as $k => $v){
                    if(!in_array($v['url'], $temp)){
                        unset($arr_id[$k]);
                    }
                }
            }
            #将权限写入session
            session("arr_id", $arr_id);
        }
    }
}
